package com.wolfking.converter.transform.extend.special;

import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wolfking.converter.transform.AbstractExtendTransform;
import com.wolfking.converter.transform.extend.BankNameTransform;
import com.wolfking.converter.transform.extend.mask.BankCardMaskTransform;

import java.util.List;
import java.util.Objects;

public class RemittanceTransform extends AbstractExtendTransform<String> {
    @Override
    public String transform(Object value) throws Exception {
        if (Objects.isNull(value)) {
            return "";
        }
        try {
            String format = "%s(%s)";
            String s = value.toString();
            JsonElement element = JsonParser.parseString(s);
            JsonArray array = element.getAsJsonArray();
            List<String> list = Lists.newArrayList();
            for (int i = 0; i < array.size(); i++) {
                JsonObject object = array.get(i).getAsJsonObject();
                String remittanceBank = object.get("remittanceBank").getAsString();
                String remittanceAccount = object.get("remittanceAccount").getAsString();
                remittanceBank = new BankNameTransform().transform(remittanceBank);
                remittanceAccount = new BankCardMaskTransform().transform(remittanceAccount);
                list.add(String.format(format, remittanceBank, remittanceAccount));
            }
            return String.join("<BR>", list);
        } catch (Exception e) {
            return "";
        }
    }
}
